<?php
/** 
 * Plugin 'act_mail' for the 'ameos_formidable' extension.
 *
 * @author	Jerome Schneider <typo3dev@ameos.com>
 */


require_once(PATH_t3lib . "class.t3lib_htmlmail.php");

class tx_actmail extends formidable_mainactionlet {

	function _doTheMagic($aRendered, $sForm) {

		$aCreation	= $this->_navConf("/creation/");
		$aEdition	= $this->_navConf("/edition/");

		if($this->oForm->oDataHandler->_allIsValid()) {

			if($this->oForm->oDataHandler->_edition() && empty($this->oForm->oDataHandler->newEntryId)) {
				// FORMidable is in edition mode
				
				if($aEdition) {

					$this->oForm->_debug(null, "ACTIONLET MAIL - INITIALIZING IN EDITION MODE");
					$this->_sendMail($aEdition);

				} else {
					$this->oForm->_debug(null, "ACTIONLET MAIL - NOT INITIALIZING COZ' IN EDITION MODE");
				}
			} else {
				// FORMidable is in creation mode

				if($aCreation) {
					$this->oForm->_debug(null, "ACTIONLET MAIL - INITIALIZING IN CREATION MODE");
					$this->_sendMail($aCreation);
				} else {
					$this->oForm->_debug(null, "ACTIONLET MAIL - NOT INITIALIZING COZ' IN CREATION MODE");
				}
			}
		}
	}
	
	function _sendMail($aConf) {

		if($this->oForm->_navConf("/addresses/userobj/", $aConf) !== FALSE)
		{ $aAddresses = $this->oForm->_callUserObj($this->oForm->_navConf("/addresses/", $aConf));}
		else
		{ $aAddresses = t3lib_div::trimExplode(",", $this->oForm->_navConf("/addresses/", $aConf));}

		if(is_array($aAddresses))
		{ $aAddresses = array_flip(array_change_key_case(array_flip($aAddresses), CASE_LOWER));}
		else
		{ $aAddresses = array($aAddresses);}

		if(is_array($aAddresses) && sizeof($aAddresses) > 0)
		{
			$aHRData = $this->oForm->oDataHandler->_getHumanFormData();
			$aHRData["labels"][$this->oForm->_navConf("/control/datahandler/keyname")] = $this->oForm->_navConf("/control/datahandler/keyname");
			$aHRData["values"][$this->oForm->_navConf("/control/datahandler/keyname")] = $this->oForm->oDataHandler->_currentEntryId();

			if($this->oForm->_navConf("/template/userobj/", $aConf) !== FALSE)
			{ $sMail = $this->oForm->_callUserObj($this->oForm->_navConf("/template/", $aConf), array("data" => $this->oForm->oDataHandler->_getFormData(), "human_readable_data" => $aHRData));}
			else
			{
				if($this->oForm->_navConf("/template/path/userobj/", $aConf) !== FALSE)
				{ $template_path = $this->oForm->_callUserObj($this->oForm->_navConf("/template/path/", $aConf));}
				else
				{ $template_path = t3lib_div::getFileAbsFileName($this->oForm->_navConf("/template/path/", $aConf));}

				$form_subpart = $this->oForm->_navConf("/template/subparts/form/", $aConf);

				if($this->oForm->isRunneable($form_subpart)) {
					$form_subpart = $this->oForm->callRunneable($form_subpart);
				}

				$sMail = $this->_parseTemplate($template_path, $form_subpart, $aHRData["labels"], $aHRData["values"]);
			}
			
			$this->oForm->_debug(array("conf" => $aConf, "human_readable_data" => $aHRData), "ACTIONLET MAIL - EXECUTION");
			
			$isHtml		= ($this->oForm->_isTrue("/html/", $aConf));
			
			$subject	= $this->oForm->_getLLLabel($this->oForm->_navConf("/subject/", $aConf));
			
			if($this->oForm->_navConf("/subject/userobj/", $aConf) !== FALSE)
			{ $subject = $this->oForm->_callUserObj($this->oForm->_navConf("/subject/", $aConf));}
			else
			{ $subject = $this->oForm->_getLLLabel($this->oForm->_navConf("/subject/", $aConf));}
			
			if($this->oForm->_navConf("/fromemail/userobj/", $aConf) !== FALSE)
			{ $fromEmail = $this->oForm->_callUserObj($this->oForm->_navConf("/fromemail/", $aConf));}
			else
			{ $fromEmail = $this->oForm->_navConf("/fromemail/", $aConf);}
			
			if($this->oForm->_navConf("/fromname/userobj/", $aConf) !== FALSE)
			{ $fromName = $this->oForm->_callUserObj($this->oForm->_navConf("/fromname/", $aConf));}
			else
			{ $fromName = $this->oForm->_getLLLabel($this->oForm->_navConf("/fromname/", $aConf));}

		
			if($this->oForm->_navConf("/replytoemail/userobj/", $aConf) !== FALSE)
			{ $replyToEmail = $this->oForm->_callUserObj($this->oForm->_navConf("/replytoemail/", $aConf));}
			else
			{ $replyToEmail = $this->oForm->_navConf("/replytoemail/", $aConf);}
			
			if($this->oForm->_navConf("/replytoname/userobj/", $aConf) !== FALSE)
			{ $replyToName = $this->oForm->_callUserObj($this->oForm->_navConf("/replytoname/", $aConf));}
			else
			{ $replyToName = $this->oForm->_getLLLabel($this->oForm->_navConf("/replytoname/", $aConf));}

			
			$oMail = t3lib_div::makeInstance('t3lib_htmlmail');
			$oMail->start();
			$oMail->useBase64();

			$oMail->subject			= $subject;
			$oMail->from_email		= $fromEmail;
			$oMail->from_name		= $fromName;
			$oMail->replyto_email	= $replyToEmail ? $replyToEmail : $fromEmail;
			$oMail->replyto_name	= $replyToName ? $replyToName : $fromName;
			$oMail->organisation	= '';
			$oMail->priority		= 3;


			if($isHtml)
			{
				// HTML
				$oMail->theParts['html']['content'] = $sMail;	// Fetches the content of the page
				$oMail->theParts['html']['path'] = '';
				$oMail->extractMediaLinks();
				$oMail->extractHyperLinks();
				$oMail->fetchHTMLMedia();
				$oMail->substMediaNamesInHTML(0);	// 0 = relative
				$oMail->substHREFsInHTML();
				$oMail->setHTML($oMail->encodeMsg($oMail->theParts['html']['content']));
			}
			else
			{
				// PLAIN
				$oMail->addPlain($sMail);
			}
			
			// SET Headers and Content
			$oMail->setHeaders();
			$oMail->setContent();
			$oMail->setRecipient($aAddresses);

			$this->oForm->_debug(get_object_vars($oMail), "MAIL OBJECT DUMP");
			$oMail->sendTheMail();
		}
		else
		{ $this->oForm->_debug(null, "ACTIONLET MAIL - NO MAIL ADDRESS GIVEN, MAIL NOT SENT");}
	}
	
	function _parseTemplate($templatePath, $templateMarker, $aTagsLabels = array(), $aTagsValues = array()) {
		// $tempUrl : the path of the template for use with t3lib_div::getUrl()
		// $tempMarker :  the template subpart marker
		// $aTags : the marker array for substitution
		
		$templateCode = t3lib_div::getUrl($templatePath);
		$template = t3lib_parsehtml::getSubpart($templateCode, $templateMarker);
		
		reset($aTagsLabels);
		while(list($elementname, $valeur) = each($aTagsLabels))
		{
			$tag = "[" . $elementname . "]";
			$template = str_replace($tag, $valeur, $template);
		}
		
		$template = ereg_replace("\[[a-zA-Z0-9_]*]", "", $template);
		
		reset($aTagsValues);
		while(list($elementname, $valeur) = each($aTagsValues))
		{
			$tag = "{" . $elementname . "}";
			$template = str_replace($tag, $valeur, $template);
		}
		
		return ereg_replace("\{[a-zA-Z0-9_]*}", "", $template);
	}
}

if (defined("TYPO3_MODE") && $TYPO3_CONF_VARS[TYPO3_MODE]["XCLASS"]["ext/ameos_formidable/api/base/act_mail/api/class.tx_actmail.php"])	{
	include_once($TYPO3_CONF_VARS[TYPO3_MODE]["XCLASS"]["ext/ameos_formidable/api/base/act_mail/api/class.tx_actmail.php"]);
}

?>